local toggle = {};
toggle.on = function() 
	if (LoggingCombat()) then
		print(OStext.."Combat logging is currently enabled.")
	else
		print(OStext.."Combat logging is now enabled.")
		LoggingCombat(1)
	end
end
toggle.off = function() 
	if not (LoggingCombat()) then
		print(OStext.."Combat logging is currently disabled.")
	else
		print(OStext.."Combat logging is now disabled.")
		LoggingCombat(0)
	end
end

local f = CreateFrame("Frame", "OS_CombatLogger")
f:RegisterEvent("ADDON_LOADED")

local function EnableAutoLog()
	f:RegisterEvent("PLAYER_ENTERING_WORLD")
	f:SetScript("OnEvent", function(self,event,...)
		if event == "PLAYER_ENTERING_WORLD" then
			local inInstance, itype = IsInInstance()
			if inInstance and itype == "raid" and not (LoggingCombat()) then
				LoggingCombat(1)
			else
				LoggingCombat(0)
			end
		end
	end)
	local inInstance, itype = IsInInstance()
	if inInstance and itype == "raid" and not (LoggingCombat()) then
		LoggingCombat(1)
	else
		LoggingCombat(0)
	end
end

local function DisableAutoLog()
	f:UnregisterEvent("PLAYER_ENTERING_WORLD")
	f:SetScript("OnEvent", nil)
end

f:SetScript("OnEvent", function(self,event,...) 
	local arg1, arg2 = ...
	if event == "ADDON_LOADED" and arg1 == "OverSimplified" then
		if OverSimpDB.combatlog then
			EnableAutoLog()
		else
			DisableAutoLog()
		end
		self:UnregisterEvent("ADDON_LOADED")
	end
end)

toggle.auto = function()
	if OverSimpDB.combatlog then
		OverSimpDB.combatlog = false
		DisableAutoLog()
		if LoggingCombat() then
			print(OStext.."Auto Combat Logging now disabled, but combat is still being logged.\nType '/cl off' to stop logging combat.")
		else
			print(OStext.."Auto Combat Logging now disabled.")
		end
	else
		OverSimpDB.combatlog = true
		EnableAutoLog()
		print(OStext.."Auto Combat Logging now enabled.")
	end
end

function osCL(arg)
	if type(toggle[arg]) == 'function' then 
		toggle[arg]()
	else 
		print(OStext.."Available commands are /cl 'on', 'off', or 'auto'.")
	end
end

SLASH_CL1 = "/cl";
SlashCmdList["CL"] = osCL